@include('admin::layouts.header')
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>管理员列表</legend>
    </fieldset>
	<div class="demoTable layui-row">
        <div class="layui-form layui-form-pane" style="float:left;">
			<div class="layui-form-item">
				<label class="layui-form-label">快捷查询</label>
				<div class="layui-input-inline layui-form">
					<input class="layui-input" name="key" id="key" placeholder="请输入用户名">
				</div>
			</div>
        </div>
		<div class="layui-form layui-form-pane" style="float:left;width:460px">
			<div class="layui-form-item">
				<label class="layui-form-label">添加时间</label>
				<div style="width:72%" class="layui-input-inline layui-form">
					<input style="width:100%" type="text" class="layui-input" id="created_at" placeholder="请选择时间">
				</div>
			</div>	
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">状态</label>
				<div class="layui-input-inline layui-form">
					<select id="status_d">
						<option value="">请选择</option>
						@foreach($status as $key=>$value)
							<option value="{{$key}}">{{$value}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">所属权限组</label>
				<div class="layui-input-inline layui-form">
					<select id="group_id">
						<option value="">请选择</option>
						@foreach($auth_groups as $auth_group)
							<option value="{{$auth_group['id']}}">{{$auth_group['title']}}</option>
						@endforeach
					</select>
				</div>
			</div>		
		</div>
    </div>
	<div class="demoTable layui-row">
		<button class="layui-btn" style="margin-left:30px;margin-top:15px;" id="search" data-type="reload">查询</button>
		<a href="{{route('admin.admin.index')}}" style="margin-top:15px;" class="layui-btn">重置</a>
	</div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
@include('admin::layouts.footer')
<script type="text/html" id="barDemo">
    <a href="/admin/admin/@{{d.id}}/edit" class="layui-btn layui-btn-xs">编辑</a>
    @{{# if(d.id==1){ }}  
	<a class="layui-btn layui-btn-xs layui-btn-disabled">密码初始化</a>
    <a class="layui-btn layui-btn-xs layui-btn-disabled">删除</a>
    @{{# }else{  }}
	<a class="layui-btn layui-btn-xs layui-btn-warm" onclick="update_pwd(@{{d.id}})">密码初始化</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    @{{# } }}
</script>
<script type="text/html" id="status">
	@{{# if(d.id==1){ }}
        <input type="checkbox" disabled name="status" value="@{{d.id}}" lay-skin="switch" lay-text="{!! config('admin.enabled') !!}|{!! config('admin.forbidden') !!}" lay-filter="status" @{{ d.status == 1 ? 'checked' : '' }}>
	@{{# }else{  }}
	<input type="checkbox" name="status" value="@{{d.id}}" lay-skin="switch" lay-text="{!! config('admin.enabled') !!}|{!! config('admin.forbidden') !!}" lay-filter="status" @{{ d.status == 1 ? 'checked' : '' }}> 
	@{{# } }}
</script>
<script type="text/html" id="topBtn">
   <a href="{{route('admin.admin.create')}}" class="layui-btn layui-btn-sm">添加管理员</a>
</script>
<script>
    layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
            elem: '#list',
            url: "{{route('admin.admin.data')}}",
            method:'get',
			toolbar: '#topBtn',
			title:'管理员列表',
			page: true,
            cols: [[
                {field:'id', title: '编号', width:100,align: 'center',fixed: true,sort: true}
                ,{field:'username', title: '用户名',align: 'center', width:200}
                ,{field:'title', title: '所属权限组',align: 'center', width:200}
                ,{field:'created_at', title: '添加时间',align: 'center', width:200,sort: true}
                ,{field:'status', title: '状态',width:150,align: 'center',toolbar: '#status'}
                ,{width:300, title: '操作',align:'center',align: 'center', toolbar: '#barDemo'} 
            ]],
			limit: "{!! config('admin.common_limit') !!}",
			parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
				  for(var i=0;i<res.data.length;i++){
					  res.data[i].title = res.data[i].auth_groups.title;
				  }
				  return res;
			}	
        });
		//搜索
        $('#search').on('click', function () {
			var created_at 		= $('#created_at').val();
			var status  		= $('#status_d').val();
			var key  		    = $('#key').val();
			var group_id  		= $('#group_id').val();
            if ($.trim(created_at) === '' && $.trim(status) === '' && $.trim(key) === '' && $.trim(group_id) === ''){
                layer.msg("{!! config('admin.query_condition') !!}", {icon: 0});
                return;
            }
			tableIn.reload({
				where: {created_at:created_at,status:status,key:key,group_id:group_id}
			});
        });
		update_pwd = function(id){
			layer.confirm('确定要初始化该管理员的密码吗！', function(index){
				$.post('/admin/admin/'+id+'/update_pwd',{_method:'PUT'},function(res){
					if(res.status == 1){
						layer.msg(res.msg,{time:1000,icon:1});
					}else{
						layer.msg(res.error,{time:1000,icon:2});
					}
				});
				layer.close(index);
			});
		};
        form.on('switch(status)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var status = obj.elem.checked===true?1:0;
            $.post('/admin/admin/'+id+'/status',{_method:'PUT',status:status},function (res) {
				layer.close(loading);
				if (res.status == 1) {  
						layer.msg(res.msg,{time:1000,icon:1});
				}else{
				   layer.msg(res.error,{time:1000,icon:2},function(){
						location.href = res.url;
						});
					return false;
				}
            })
        });
    });
</script>
</body>
</html>
@include('admin::layouts.delete_ajax',['url'=>route('admin.admin.destroy')])